package controllers

import (
	"goblog/models/dao"
	"github.com/astaxie/beego"
	"strconv"
	"strings"
)

type ArticleController struct {
	beego.Controller
}

func (this *ArticleController) Get() {
	o := new(dao.ArticleDao)
	id, _ := strconv.Atoi(this.Ctx.Input.Param(":id"))
	article := o.Get(id)
	this.Data["article"] = article
	article.Views = article.Views + 1;
	o.PlusViews(id)
	this.Data["tags"] = strings.Split(article.Tags, " ")
	this.TplName = "article.html"
	this.Layout = "layout/layout.html"
	SetCatList(this.StartSession(), this.Data)
}

func (c *ArticleController) List() {
	sPage := c.Input().Get("page")
	title := c.Input().Get("title")

	page, _ := strconv.Atoi(sPage)
	if page == 0 {
		page = 1
	}
	o := new(dao.ArticleDao)
	term := new(dao.ArticleTerm)
	term.Size = 10
	term.Page = page
	term.Title = title
	o.Find(term)
	c.Data["json"] = term;
	c.ServeJSON()
}
